<template>
	<view class="fl-s-s p-r-30">
		<text class="open-title fm-ali c-33 fs-28 m-t-30">直播间名称</text>
		<input v-model="live_name" class="myinput w-1 m-t-40 fs-32" placeholder-class="c-99" type="text" placeholder="请输入直播间名称">
		<text class="open-title fm-ali c-33 fs-28 m-t-60">上传直播封面图</text>
		<view v-if="!src" @click="chooseImage" class="chooseimg m-t-30 fr-c" mode="aspectFill" src="/static/images/user/pj_chooseImage.png">
			<image src="/static/images/home/open_living_add.png" class="add_img"></image>
		</view>
		<image v-if="src" @click="chooseImage" class="chooseimg m-t-30" mode="aspectFill" :src="src"></image>
		
		<view class="iphonex fr-c" style="background: #fff;">
			<view class="fr-c sub-btn" @click="subOpen">通知会员</view>
		</view>
	</view>
</template>

<script>
	import TIM from 'tim-wx-sdk';
	import tim from '@/utils/tim.js'
	export default {
		data() {
			return {
				src:'',
				live_name:'',
				live_id:'',
				group_id:''
			}
		},
		onLoad() {
			this.getInfo()
			this.getImInfo()
		},
		methods: {
			getImInfo(){
				this.$get({
					url:'/homepage/getImUser',
					data:{
						access_token:uni.getStorageSync('access_token')
					}
				}).then(res=>{
						tim.login({userID: String(res.data.userId), userSig: res.data.userSig}).then((imres)=>{
							console.log(imres.data,'登录成功'); // 登录成功
							if (imres.data.repeatLogin === true) {
							  // 标识账号已登录，本次登录操作为重复登录。v2.5.1 起支持
							  console.log(imres.data.errorInfo,'重复登录了');
							}
						}).catch(err=>{
							console.log(err,'登录失败')
						})
				})
			},
			// 获取直播信息
			getInfo(){
				this.$get({
					url:'/homepage/getMyLive',
					data:{
						access_token:uni.getStorageSync('access_token')
					}
				}).then(res=>{
					this.live_name = res.detail.live_name
					this.src = res.detail.live_img
				})
			},
			async chooseImage(){
				//#ifdef APP-PLUS				let result = await this.$store.dispatch("quanxian/requestPermissions",'WRITE_EXTERNAL_STORAGE')					console.log(result)				if (result !== 1) return
				this.$store.state.isCAMERA = true				// #endif
				
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album','camera'], //从相册选择
					success: (res) => {
						this.src = res.tempFilePaths[0]
					}
				})
			},
			// 退出
			quitGroup(group_id){
				let promise = tim.quitGroup(group_id);
				promise.then(function(imResponse) {
				  console.log(imResponse.data.groupID,'退出直播间'); // 退出成功的群 ID
				}).catch(function(imError){
				  console.warn('quitGroup error:', imError); // 退出群组失败的相关信息
				});
			},
			// 解散直播间 群
			dismissGroup(group_id){
				tim.dismissGroup(group_id).then((imResponse) => { // 解散成功
					console.log(imResponse.data.groupID); // 被解散的群组 ID
					// 关闭直播间
					this.$get({
						url:'/homepage/closeLive',
						data:{
							live_id:this.live_id
						},
						loading:false
					})
				}).catch(function(imError) {
					console.warn('dismissGroup error:', imError); // 解散群组失败的相关信息
				});
			},
			async subOpen(){
				if(!this.live_name) return this.$u.toast('请输入直播间名称');
				if(!this.src) return this.$u.toast('请选择直播间封面图');
				const imgres = await this.$up({
					imageUrl:[...[this.src]]
				})
				// this.src = imgres[0]
				this.$post({
					url:'/homepage/startLive',
					data:{
						access_token:uni.getStorageSync('access_token'),
						live_name:this.live_name,
						live_img:imgres[0]
					}
				}).then(res=>{
					this.group_id = res.data.group_id
					this.live_id = res.data.live_id
					uni.showToast({
						title:res.msg,
						mask:true
					})
					
					setTimeout(()=>{
						this.$get({
							url:'/homepage/getImUser',
							data:{
								access_token:uni.getStorageSync('access_token')
							},
							loading:false
						}).then(tres=>{
								tim.login({
									userID: String(tres.data.userId),
									userSig: tres.data.userSig
								}).then((imres)=>{
									console.log(imres.data,'登录成功'); // 登录成功
									tim.joinGroup({
										 groupID: this.group_id,
										 type: TIM.TYPES.GRP_AVCHATROOM
									}).then(joinres=>{
										switch (joinres.data.status) {
										  case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL: // 等待管理员同意
												console.log('等待管理员审核'); // 加入的群组资料
										    break;
										  case TIM.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
												console.log('加群成功'); // 加入的群组资料
												uni.navigateTo({
													// url:'./OpenDetail/OpenDetail?id='+res.live_id
													url:'/pages/Home/LivingList/OpenLiving/OpenDetail/playLiving?id='+this.live_id + '&group_id='+this.group_id
												})
										    break;
										  case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
												console.log('已经在群中'); // 加入的群组资料
												uni.navigateTo({
													// url:'./OpenDetail/OpenDetail?id='+res.live_id
													url:'/pages/Home/LivingList/OpenLiving/OpenDetail/playLiving?id='+this.live_id + '&group_id='+this.group_id
												})
										    break;
										  default:
										    break;
										}
									})
								}).catch(err=>{
									console.log(err,'登录失败')
								})
						})
					},1500)
				})
			},
		}
	}
</script>

<style>
.open-title:before{
	content: '*';
	color: #FF1E1E;
	font-size: 28rpx;
}
.myinput{
	box-sizing: content-box;
	padding-bottom: 24rpx;
	border-bottom: 2rpx solid #E4E4E4;
}

.chooseimg{
	border-radius: 12rpx;
	width: 276rpx;
	height: 250rpx;
	background: #e4e4e4;
}

.sub-btn{
		width: 686rpx;
		height: 100rpx;
		background: linear-gradient(180deg, #E46C30 0%, #E32C1C 100%);
		/* box-shadow: 0rpx 6rpx 32rpx 2rpx rgba(250, 74, 20, 0.58); */
		border-radius: 50rpx;
		font-size: 32rpx;
		font-family: Alibaba;
		color: #FFFFFF;
		margin-bottom: 30rpx;
	}
	.add_img{
		width: 96rpx;
		height: 90rpx;
	}
</style>
